#pragma once
#include <iostream>
#include <string>
#include <queue>
#include <memory>
#include <vector>
#include <unistd.h>
#include "Mutex.hpp"
#include "Log.hpp"
#include "Cond.hpp"
#include "Thread.hpp"
using namespace ThreadModule;
using namespace CondModule;
using namespace LogModule;
using namespace LockModule;
void Test()
{
    while (true)
    {
        sleep(1);
        LOG(LogLevel::DEBUG) << "测试信息";
    }
}
namespace ThreadPoolModule
{
    using thread_t = std::shared_ptr<Thread>;
    const static int defaultnum = 5;
    template <typename T>
    class ThreadPool
    {
        bool IsEmpty()
        {
            return _taskq.size() == 0;
        }

    private:
        void HandlerTask(std::string name)
        {
            LOG(LogLevel::INFO) << "线程:" << name << "进入HandlerTask的逻辑";
            while (true)
            {
                // 拿任务
                T t;
                {
                    LockGuard lockguard(_lock);

                    while (IsEmpty() && IsRunning)
                    {
                        _wait_num++;
                        _cond.Wait(_lock);
                        _wait_num--;
                    }
                    if (IsEmpty())
                        break;
                    t = _taskq.front();
                    _taskq.pop();
                }
                // 处理任务
                t(name); // 规定所有任务都需要提供无参方法
            }
            LOG(LogLevel::INFO) << "线程" << name << " 退出";
        }
        ThreadPool(const ThreadPool<T> &) = delete;
        const ThreadPool<T> &operator=(const ThreadPool<T> &) = delete;
        ThreadPool(int num = defaultnum)
            : _num(num), _wait_num(0), IsRunning(0)
        {
            for (int i = 0; i < num; i++)
            {
                {
                    _threads.push_back(std::make_shared<Thread>(std::bind(&ThreadPool::HandlerTask, this, std::placeholders::_1)));
                    LOG(LogLevel::INFO) << "构建线程" << _threads.back()->Name() << "...成功";
                }
            }
        }

    public:
        static ThreadPool<T> *getinstance()
        {
            LockGuard lockguard(_mutex);
            if (instance == nullptr)
            {
                LOG(LogLevel::INFO) << "单例首次被执行需要加载对象";
                instance = new ThreadPool<T>();
            }
            return instance;
        }
        ~ThreadPool() {}
        void Start()
        {
            if (IsRunning)
                return;
            IsRunning = true;
            for (auto &i : _threads)
            {
                {
                    i->Start();
                    LOG(LogLevel::INFO) << "启动线程" << i->Name() << "...成功";
                }
            }
        }
        void Wait()
        {
            for (auto &i : _threads)
            {
                i->Join();
                LOG(LogLevel::INFO) << "回收线程" << i->Name() << "...成功";
            }
        }
        void Stop()
        {
            LockGuard lockguard(_lock);
            if (!IsRunning)
                return;
            LOG(LogLevel::INFO) << "停止线程池";
            // 退出线程池要保证历史任务处理完了
            // 不能再入任务了
            IsRunning = false;
            if (_wait_num > 0)
                _cond.NotifyAll();
        }

        void Equeue(T &&in)
        {
            LockGuard lockguard(_lock);
            if (!IsRunning)
                return;
            _taskq.push(std::move(in));
            if (_wait_num > 0)
                _cond.Notify();
        }

    private:
        int _num;             // 线程数量
        std::queue<T> _taskq; // 任务队列
        std::vector<thread_t> _threads;
        Mutex _lock;
        Cond _cond;
        int _wait_num;
        bool IsRunning;
        static ThreadPool<T> *instance;
        static Mutex _mutex;
    };
    template <typename T>
    ThreadPool<T> *ThreadPool<T>::instance = nullptr;
    template <typename T>
    Mutex ThreadPool<T>::_mutex;
}